# !/usr/bin/env python
# -*-coding:utf-8 -*-

"""
# File       : task_group_view.py
# Time       ：2024/9/8 17:21
# Author     ：author name
# version    ：python 3.10
# Description：任务组修改视图
"""
from flask_restx import fields

from apis.orderlines import orderlines_ns
from apis.orderlines.models import Task
from public.api_utils.api_decorator import handle_api_error
from public.base_model import db
from public.base_response import generate_response, MarshalModel
from public.base_view import BaseView

task_group = {
    'task_id': fields.String(description='任务ID'),
    'method_kwargs': fields.Raw(description='任务执行参数'),
}

task_group_marshal = MarshalModel('task_group', orderlines_ns)


class TaskGroupView(BaseView):
    url = '/task_group'

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

    @handle_api_error()
    @orderlines_ns.doc(description='任务组参数修改')
    @orderlines_ns.expect(task_group_marshal.detail_request(task_group))
    def put(self):
        """任务组参数修改"""
        with db.auto_commit():
            db.session.query(Task).filter(
                Task.task_id == self.form_data.get('task_id'), Task.active == 1
            ).update(self.form_data)
            db.session.commit()

        return generate_response(message='任务组参数修改成功')
